/**
 * [@cms_listsites;site,index]${index}.${site.sitename}[/@cms_listsites]
 */

CKEDITOR.plugins.add( 'cmstag',
{
	init: function( editor )
	{
		var command=editor.addCommand( 'cmstagDialog', new CKEDITOR.dialogCommand( 'cmstagDialog' ) );
		command.modes = { wysiwyg:1, source:1 };
		command.canUndo = false;
		command.readOnly = 1;
		
		editor.ui.addButton( 'CmsTag',
		{
			label: '模板标签库',
			command: 'cmstagDialog',
			icon: this.path + 'images/icon.png'
		} );
		CKEDITOR.dialog.add( 'cmstagDialog', function( editor )
		{
			return {
				title : '模板标签库',
				minWidth : 500,
				minHeight : 400,
				contents :
				[
					{
						// Definition of the Settings dialog window tab (page) with its id, label and contents.
						// http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dialog.contentDefinition.html
						id : 'tags',
						label : '标签列表',
						elements :
						[
							{
								type : 'html',
								html : '此处列出了可用于模板的所有标签，并提供了标签使用样例，模板开发人员可以复制样例并应用于模板中。'
							},
							{
								type : 'hbox',
								children :
								[
									{
										type : 'vbox',
										width:'120px',
										children :
										[
											{
												type : 'select',
												id : 'taglist',
												label : '标签列表',
												size:26,
												style:'width:120px',
												items : [
											         [ '站点列表','site_list' ], 
											         [ '栏目列表','channel_list' ],
											         [ '分页文章列表','info_list' ],
											         [ '前几条文章列表','topinfo_list' ],
											         [ '相关文章列表','related_list' ],
											         [ '分页条','page'],
											         [ '导航条','navigation'],
											         ['文本切割','cuttext'],
											         ['JS标签','js']
												],
												onChange : function( api ) {
													// this = CKEDITOR.ui.dialog.select
													//alert(this.getDialog().getContentElement( 'tags', 'taglist' ).getValue());
													var str;
													switch(this.getValue())
													{
														case 'site_list':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_listsites;siteList]');
															this.getDialog().getContentElement( 'tags', 'tagsample').setValue('[@cms_listsites;siteList]\n    [#list siteList as site]\n        <a href="${site.url}">${site_index}.${site.sitename}</a>\n    [/#list]\n[/@cms_listsites]');
															break;
														case 'channel_list':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_listchannels;channelList]');
															str='参数:\nsiteid:站点id不设值时,取当前站点id。\npid:父栏目id，不设值时,默认值为0，取顶级栏目列表。\n\n';
															str+='[@cms_listchannels;channelList]\n    [#list channelList as channel]\n        <a href="${channel.url}" target="${channel.target}">${channel_index}.${channel.channelname}</a>\n    [/#list]\n[/@cms_listchannels]\n\n';
															str+='[@cms_listchannels siteid=1 pid=0;channelList]\n    [#list channelList as channel]\n        <a href="${channel.url}" target="${channel.target}">${channel_index}.${channel.channelname}</a>\n    [/#list]\n[/@cms_listchannels]';
															this.getDialog().getContentElement( 'tags', 'tagsample').setValue(str);
															break;
														case 'info_list':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_listinfos;infoList]');
															str='参数:\nchannelid:栏目id，不设值时，取当前栏目id。\nrecursive:是否递归查询子栏目下文章，不设值时，默认值为false，仅取当前栏目下的文章，设为true时，取当前栏目及所有下属子栏目的文章。\n';
															str+='page:页码，不设值时默认值为1。\n';
															str+='rows:行数，不设值时默认值为10。\n';
															str+='infotype:信息类型，不设值时默认值为0。0:所有 1:图片信息 2:推荐信息。\n';
															str+='ordertype:排序类型，不设值时默认值为0。0:按发布时间倒序  1:按发布时间升序 2:按点击量倒序 3:按点击量升序	。\n\n';
															str+='[@cms_listinfos;infoList]\n    [#list infoList as info]\n        ${info_index}.${info.title}\n    [/#list]\n[/@cms_listinfos]\n\n';
															str+='[@cms_listinfos channelid=1 recursive=true infotype=0;infoList]\n    [#list infoList as info]\n        ${info_index}.${info.title}\n    [/#list]\n[/@cms_listinfos]';
															this.getDialog().getContentElement('tags','tagsample').setValue(str);
															break;
														case 'topinfo_list':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_topinfos;infoList]');
															str='参数:\nchannelid:栏目id，不设值时，取当前栏目id。\nrecursive:是否递归查询子栏目下文章，不设值时，默认值为false，仅取当前栏目下的文章，设为true时，取当前栏目及所有下属子栏目的文章。\n';
															str+='maxresults:返回信息的最大条数。不设值时，默认为10。\n';
															str+='infotype:信息类型，不设值时默认值为0。0:所有 1:图片信息 2:推荐信息。\n';
															str+='ordertype:排序类型，不设值时默认值为0。0:按发布时间倒序  1:按发布时间升序 2:按点击量倒序 3:按点击量升序	。\n\n';
															str+='[@cms_topinfos;infoList]\n    ......\n[/@cms_topinfos]\n\n';
															str+='[@cms_topinfos channelid=1 recursive=true infotype=0;infoList]\n    [#list infoList as info]\n        ${info_index}.${info.title}\n    [/#list]\n[/@cms_topinfos]';
															this.getDialog().getContentElement('tags','tagsample').setValue(str);
															break;
														case 'related_list':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_listrelated;infoList]');
															str='参数:\ninfoid:文章id，不设值时，取当前文章id。\n';
															str+='maxresults:返回信息的最大条数。不设值时，默认为10。\n\n';
															str+='[@cms_listrelated;infoList]\n    ......\n[/@cms_listrelated]\n\n';
															str+='[@cms_listrelated infoid=1 maxresults=10;infoList]\n    [#list infoList as info]\n        ${info_index}.${info.title}\n    [/#list]\n[/@cms_listrelated]';
															this.getDialog().getContentElement('tags','tagsample').setValue(str);
															break;
														case 'page':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_page;pageBean]');
															str='参数:\nchannelid:栏目id，不设值时，取当前栏目id。\nrecursive:是否递归查询子栏目下文章，不设值时，默认值为false，仅取当前栏目下的文章，设为true时，取当前栏目及所有下属子栏目的文章。\n';
															str+='page:页码，不设值时默认值为1。\n';
															str+='rows:行数，不设值时默认值为10。\n';
															str+='infotype:信息类型，不设值时默认值为0。0:所有 1:图片信息 2:推荐信息。\n\n';
															str+='[@cms_page channelid=1 recursive=true;pageBean]\n    ......\n[/@cms_page]\n\n';
															str+='[@cms_page recursive=true infotype=0;pageBean]\n    [@cms.page pageBean=pageBean/]\n[/@cms_page]';
															this.getDialog().getContentElement('tags','tagsample').setValue(str);
															break;
														case 'navigation':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_navigation;channel]');
															str="<a href='${basePath}'>首页</a>[@cms_navigation;channel] &gt; <a href='${channel.url}'>${channel.channelname}</a>[/@cms_navigation]";
															this.getDialog().getContentElement('tags','tagsample').setValue(str);
															break;
														case 'cuttext':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_cuttext]');
															str="参数:\ntext:被截取的文本。\n";
															str+="length:要截取的长度，不设值时，默认值为50。\n注意:中文字符算2个字符长度！\n\n";
															str+="[@cms_cuttext text='abcde你好' length=50/]";
															this.getDialog().getContentElement('tags','tagsample').setValue(str);
															break;
														case 'js':
															this.getDialog().getContentElement( 'tags', 'tagname').setValue('[@cms_js]');
															str="js标签用来加载jquery等常用js文件，包含:\n";
															str+="    /resources/js/validate.js\n";
															str+="    /resources/js/jquery/jquery.min.js\n";
															str+="    /resources/js/jquery/jquery.form.js\n";
															str+="    /resources/js/jquery/jquery.selectboxes.js\n";
															str+="    /resources/js/flvplayer/jwplayer.js\n\n";
															str+="[@cms_js/]";
															this.getDialog().getContentElement('tags','tagsample').setValue(str);
															break;	
													}
												}
											}
										]
									},
									{
										type : 'vbox',
										width:'460px',
										children :
										[
											{
												type : 'text',
												id : 'tagname',
												label : '标签内容'
											},
											{
												type : 'textarea',
												id : 'tagsample',
												label : '标签示例',
												rows:'22',
												commit : function( data )
												{
													data.samplecode = this.getValue();
												}
											}
										]
									}
								]
							}
						]
					},
					{
						id : 'directives',
						label : '指令列表',
						elements :
						[
							{
								type : 'html',
								html : '此处列出了可用于模板的常用指令(Freemarker指令)，更多指令请参考Freemarker参考文档。'
							},
							{
								type : 'hbox',
								children :
								[
									{
										type : 'vbox',
										width:'120px',
										children :
										[
											{
												type : 'select',
												id : 'directivelist',
												label : '指令列表',
												size:26,
												style:'width:120px',
												items : [ [ 'include 指令','include' ], [ 'if else 指令','ifelse' ], [ 'switch 指令','switch' ], [ 'list 指令','list' ] ],
												onChange : function( api ) {
													// this = CKEDITOR.ui.dialog.select
													//alert(this.getDialog().getContentElement( 'tags', 'taglist' ).getValue());
													var str;
													switch(this.getValue())
													{
														case 'include':
															this.getDialog().getContentElement( 'directives', 'directivename').setValue('[#include path]');
															this.getDialog().getContentElement( 'directives', 'directivesample').setValue('[#include "head.html"]');
															break;
														case 'ifelse':
															this.getDialog().getContentElement( 'directives', 'directivename').setValue('[#if condition] [#elseif condition][#else][/#if]');
															str='[#if condition]\n    ......\n';
															str+='[#elseif condition]\n    ......\n';
															str+='[#elseif condition]\n    ......\n';
															str+='[#else]\n    ......\n';
															str+='[/#if]';
															this.getDialog().getContentElement( 'directives', 'directivesample').setValue(str);
															break;
														case 'switch':
															
															break;
														case 'list':
															
															break;
													}
												}
											}
										]
									},
									{
										type : 'vbox',
										width:'460px',
										children :
										[
											{
												type : 'text',
												id : 'directivename',
												label : '标签内容'
											},
											{
												type : 'textarea',
												id : 'directivesample',
												label : '标签示例',
												rows:'22',
												commit : function( data )
												{
													data.samplecode = this.getValue();
												}
											}
										]
									}
								]
							}
						]
					}
				],
				buttons : [ CKEDITOR.dialog.cancelButton ]
			};
		} );
	}
} );